package org.edu.modules.oa.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.edu.modules.oa.entity.VeOaMeetPlaceEntity;
import org.edu.modules.oa.mapper.IVeOaMeetPlaceMapper;
import org.edu.modules.oa.service.IVeOaMeetPlaceService;
import org.edu.modules.oa.vo.VeOaMeetPlaceTableVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
/*会议场地Server接口*/
public class VeOaMeetPlaceServiceImpI implements IVeOaMeetPlaceService {
    @Resource
    private IVeOaMeetPlaceMapper iVeOaMeetPlaceMapper;

    @Override
    public HashMap<String, Object> showTable(VeOaMeetPlaceEntity veOaMeetPlaceEntity, Integer startPage, Integer pageSize) {
        System.out.println(veOaMeetPlaceEntity);

        System.out.println(startPage+"startPage"+pageSize+"startPage");
        // 调用mybatisplus的分页
        IPage<VeOaMeetPlaceEntity> page=new Page<>(startPage,pageSize);
        // 拼接查询条件
        QueryWrapper<VeOaMeetPlaceEntity> tableWrapper = new QueryWrapper<>();
        if(veOaMeetPlaceEntity.getStatus()!=null&&veOaMeetPlaceEntity.getStatus()!=-1){
            tableWrapper.eq("status",veOaMeetPlaceEntity.getStatus());

        }
        if(veOaMeetPlaceEntity.getPlaceName()!=null&&veOaMeetPlaceEntity.getPlaceName()!="%%"){
            tableWrapper.like("place_name",veOaMeetPlaceEntity.getPlaceName());

        }
        tableWrapper.orderByDesc("create_time");
        // 条件加上分页查询
        page =iVeOaMeetPlaceMapper.selectPage(page,tableWrapper);
        List<VeOaMeetPlaceEntity> entityList=page.getRecords();

        List<VeOaMeetPlaceTableVo> voelist =new ArrayList<>();
        // for循环把需要用的字段加到 导出集合里面去
        for (VeOaMeetPlaceEntity o:entityList){
            VeOaMeetPlaceTableVo tableVo =new VeOaMeetPlaceTableVo();
            tableVo.setId(o.getId());
            tableVo.setPlaceName(o.getPlaceName());
            String state = "";
            // 判断启用还是禁用
            if(o.getStatus()==0){
                state="启用";
            }
            if(o.getStatus()==1){
                state="禁用";

            }
            tableVo.setStatus(state);
            // 时间类型转换成字符串
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            tableVo.setCreateTime(formatter.format(o.getCreateTime()));
            tableVo.setItemId(o.getItemId());
            voelist.add(tableVo);
        }
        int count =iVeOaMeetPlaceMapper.selectCount(tableWrapper);
        // 把查出来的东西存到map里面去
        HashMap<String , Object> map = new HashMap<>();
        map.put("list",voelist);
        map.put("count",count);

        // 查询表格总行数
        return map;
    }

    @Override
    public Integer deleteRecord(Integer id) {

        int result = iVeOaMeetPlaceMapper.deleteById(id);
        return result;
    }

    @Override
    public Integer alterState(Long id, Integer state) {

        VeOaMeetPlaceEntity entity = new VeOaMeetPlaceEntity();
        entity.setId(id);
        entity.setStatus(state);
        //根据id进行更新，没传值的属性就更新为null

        return iVeOaMeetPlaceMapper.updateById(entity);
    }

    @Override
    public Integer addPlace(VeOaMeetPlaceEntity veOaMeetPlaceEntity) {



        // 增加
        Integer result=iVeOaMeetPlaceMapper.insert(veOaMeetPlaceEntity);
        return result;
    }

    @Override
    public Integer amendPlace(VeOaMeetPlaceEntity veOaMeetPlaceEntity) {
        QueryWrapper<VeOaMeetPlaceEntity> wrapper =new QueryWrapper();
       wrapper.eq("id",veOaMeetPlaceEntity.getId());
        Integer result=  iVeOaMeetPlaceMapper.update(veOaMeetPlaceEntity,wrapper);
        return result;
    }

    @Override
    public VeOaMeetPlaceEntity epetitionrJudge(String placeName) {
        QueryWrapper<VeOaMeetPlaceEntity> wrapper =new QueryWrapper<VeOaMeetPlaceEntity>();
        wrapper.eq("place_name",placeName);

        return iVeOaMeetPlaceMapper.selectOne(wrapper);
    }
    @Override
    public List<VeOaMeetPlaceEntity> selectPlaces() {
        List<VeOaMeetPlaceEntity> list = iVeOaMeetPlaceMapper.selectList(null);
        return list;
    }
}
